package cetcbigdata.da.personal_service.sichuan;

import cetcbigdata.da.base.DownloadAndUploadMinio;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class SCUtil {
    /**
     * 基础信息
     * @param jxDocument
     * @return
     */
    public static JSONObject getBaseInfo(JXDocument jxDocument){
        JSONObject base = new JSONObject();
        List<JSONObject> baseList = new ArrayList<>();
        List<Object> trList = jxDocument.sel("//div[@id='div1']/table[1]//tr");
        for (Object o : trList){
            Elements tds = ((Element) o).children();
            for (int i = 0, len = tds.size(); i < len; i+=2) {
                JSONObject bascInfo = new JSONObject();
                String key = tds.get(i).text();
                String vaule = tds.get(i+1).text();
                bascInfo.put(key, vaule);
                baseList.add(bascInfo);
            }
        }
        base.put("基础信息", baseList);
        return base;
    }

    /**
     * 审批结果
     * @param jxDocument
     * @return
     */
    public static JSONObject getApprovalInfo(JXDocument jxDocument){
        JSONObject approval = new JSONObject();
        List<Object> trList = jxDocument.sel("//div[@id='div1']/table[2]//tr[2]");
        if (trList.size() > 0){
            approval.put("审批结果类型", ((Element) trList.get(0)).child(0).text());
            approval.put("审批结果名称", ((Element) trList.get(0)).child(1).text());
            List<Object> resultList = jxDocument.sel("//*[@id='div1']/div[3]/div[2]/div[2]/table/tbody");//*[@id="div1"]/div[3]/div[2]/div[2]/table/tbody/tr[2]
            if (resultList.size() > 0){
                List<JSONObject> sampleLIst = new ArrayList<>();
                for (Object o : resultList) {
                    Elements result = ((Element) o).children();
                    for (int i = 0 ; i < result.size(); i ++) {
                        //不循环第一项
                        if (i == 0) continue;
                        JSONObject sample = new JSONObject();
                        String key = result.get(i).child(0).text();
                        String url = result.get(i).getElementsByTag("a").attr("href");
                        sample.put("名称", key);
                        sample.put("源地址", url);
                        sample.put("minioId", fileDownloadAndUploadMinioGet(url, key));
                        sampleLIst.add(sample);
                    }
                }
                approval.put("审批结果样本", sampleLIst);
            }
        }
        return approval;
    }

    /**
     * 窗口办理
     * @param jxDocument
     * @return
     */
    public static JSONObject getWinHandling(JXDocument jxDocument){
        JSONObject WinHandling = new JSONObject();
        List<JSONObject> winList = new ArrayList<>();
        List<Object> dataList = jxDocument.sel("//div[@class='section2']/table[1]//tr");
        for (Object o : dataList){
            Elements children = ((Element) o).children();
            for (int i = 0, len = children.size(); i < len;) {
                JSONObject winInfo = new JSONObject();
                String key = children.get(i).text();
                String vaule = children.get(i+1).text();
                i += 2;
                winInfo.put(key, vaule);
                winList.add(winInfo);
            }
        }
        WinHandling.put("窗口办理", winList);
        return WinHandling;
    }

    /**
     * 网上办理
     * @param jxDocument
     * @return
     */
    public static JSONObject getOnlineProcess(JXDocument jxDocument){
        JSONObject onlie = new JSONObject();
        List<JSONObject> onlieList = new ArrayList<>();
        List<Object> ObjectList = jxDocument.sel("//div[@class='section3']/table[1]//tr");
        for (Object o : ObjectList){
            Elements children = ((Element) o).children();
            for (int i = 0, len = children.size(); i < len;) {
                JSONObject onlieInfo = new JSONObject();
                String key = children.get(i).text();
                String vaule = children.get(i+1).text();
                i += 2;
                onlieInfo.put(key, vaule);
                onlieList.add(onlieInfo);
            }
        }
        onlie.put("网上办理", onlieList);
        return onlie;
    }

    /**
     * 常规信息
     * @param jxDocument
     * @return
     */
    public static JSONObject getGeneralInfo(JXDocument jxDocument){
        JSONObject general =new JSONObject();
        List<JSONObject> generalList = new ArrayList<>();
        List<Object> dataList = jxDocument.sel("//table[@class='table1']//tr");
        for (Object o : dataList){
            Elements children = ((Element) o).children();
            for (int i = 0, len = children.size(); i < len;) {
                JSONObject generalInfo = new JSONObject();
                String key = children.get(i).text();
                String vaule = children.get(i+1).text();
                i += 2;
                generalInfo.put(key, vaule);
                generalList.add(generalInfo);
            }
        }

        List<Object> DataList = jxDocument.sel("//table[@class='table2']//tr");
        for (Object o : DataList){
            Elements children = ((Element) o).children();
            for (int i = 0, len = children.size(); i < len;) {
                JSONObject generalInfo = new JSONObject();
                String key = children.get(i).text();
                String vaule = children.get(i+1).text();
                i += 2;
                generalInfo.put(key, vaule);
                generalList.add(generalInfo);
            }
        }
        general.put("常规信息", generalList);
        return general;
    }

    /**
     * 申请材料
     * @param jxDocument
     * @return
     */
    public static List<JSONObject> getApplicationMaterialsInfo(JXDocument jxDocument){
        try {
            // 基本信息
            List<JSONObject> applicationMaterialList = new ArrayList<>();
            List<Object> rs = jxDocument.sel("//div[@id='material']//tbody");
            for (Object o : rs) {
                Elements children = ((Element) o).children();
                for (int i = 0; i < children.size(); i++) {
                    JSONObject material = new JSONObject();
                    if (i == 0) continue;
                    Element element = children.get(i);
                    material.put("材料名称", element.child(1).text());
                    material.put("材料必要性", element.child(2).text());
                    material.put("材料类型", element.child(3).text());
                    material.put("材料形式", element.child(4).text());
                    material.put("纸质材料份数", element.child(5).text());

                    //材料下载
                    List<JSONObject> downLandList = new ArrayList<>();
                    List<Object> downList = jxDocument.sel("//div[@id='zhezhao']/div");
                    JSONObject blank = new JSONObject();
                    JSONObject sample = new JSONObject();
                    String blankName = null;
                    String blankFileUrl = null;
                    String sampleName = null;
                    String sampleFileUrl = null;
                    try {
                        // 空白表
                        blankName = ((Element) downList.get((i - 1) * 3)).child(1).child(1).child(0).child(0).child(1).child(0).text();
                        blankFileUrl = ((Element) downList.get((i - 1) * 3)).child(1).child(1).child(0).child(0).child(1).child(1).child(0).attr("href");
                        blank.put("名称", blankName);
                        blank.put("原地址", blankFileUrl);
                        blank.put("minioId", fileDownloadAndUploadMinioGet(blankFileUrl, blankName));
                        downLandList.add(blank);
                    } catch (Exception e) {
                        blankName = "";
                        blankFileUrl = "";
                    }
                    try {
                        // 样例表
                        sampleName = ((Element) downList.get((i - 1) * 3 + 1)).child(1).child(1).child(0).child(0).child(1).child(0).text();
                        sampleFileUrl = ((Element) downList.get((i - 1) * 3 + 1)).child(1).child(1).child(0).child(0).child(1).child(1).child(0).attr("href");
                        sample.put("名称", sampleName);
                        sample.put("源地址", sampleFileUrl);
                        sample.put("minioId", fileDownloadAndUploadMinioGet(sampleFileUrl, sampleName));
                        downLandList.add(sample);
                    } catch (Exception e) {
                        sampleName = "";
                        sampleFileUrl = "";
                    }
                    material.put("材料下载", downLandList);

                    //详情
                    List<JSONObject> detailInfo = new ArrayList<>();
                    Elements children1 = ((Element) downList.get((i - 1) * 3 + 2)).child(1).child(1).child(0).child(0).children();
                    for (int j = 0; j < children1.size(); j++) {
                        JSONObject data = new JSONObject();
                        String key = children1.get(j).child(0).text();
                        String vaule = children1.get(j).child(1).text();
                        data.put(key, vaule);
                        detailInfo.add(data);
                    }
                    material.put("详情", detailInfo);
                    applicationMaterialList.add(material);
                }
            }
            return applicationMaterialList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 受理条件
     * @param jxDocument
     * @return
     */
    public static JSONObject getAcceptance(JXDocument jxDocument){
        try {
            JSONObject acceptance = new JSONObject();
            List<Object> items = jxDocument.sel("//div[@id='div3']");
            for (Object o : items) {
                String key  = ((Element) o).child(0).text();
                String value = ((Element) o).child(1).text();
                acceptance.put(key, value);
            }
            return acceptance;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 办理流程信息
     * @param jxDocument
     * @return
     */
    public static JSONObject getProcess(JXDocument jxDocument){
        try {
            //办理流程  办理时长  转外时限
            JSONObject info = new JSONObject();
            List<Object> rs = jxDocument.sel("//div[@id='div4']");
            List<JSONObject> processList = new ArrayList<>();
            //判断是否有办理流程
            int size = ((Element) rs.get(0)).children().get(1).children().size();
            if (size == 1){
                List<Object> items = jxDocument.sel("//div[@id='div4']//tr");
                for (int i = 0; i < items.size(); i++) {
                    if (i == 0) continue;
                    JSONObject processData = new JSONObject();
                    processData.put("环节", ((Element) items.get(i)).child(0).text());
                    processData.put("办理时限", ((Element) items.get(i)).child(1).text());
                    processData.put("转外时限", ((Element) items.get(i)).child(2).text());
                    processList.add(processData);
                }
                info.put("办理流程", processList);
                info.put("办理流程说明", ((Element) items.get(1)).child(3).text());
            }else {
                info.put("办理流程", "");
                info.put("办理流程说明", "");
            }
            return info;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 收费标准
     * @param jxDocument
     * @return
     */
    public static JSONObject getCharges(JXDocument jxDocument){
        JSONObject charges = new JSONObject();
        List<Object> list = jxDocument.sel("//div[@id='div5']//p");
        String info = null;
        for (Object o : list) {
            info = ((Element) o).text();
        }
        charges.put("收费标准", info);
        return charges;
    }

    /**
     * 设定依据
     * @param jxDocument
     * @return
     */
    public static List<JSONObject> getSettingBasis(JXDocument jxDocument){
        try {
            List<JSONObject> settingBasis = new ArrayList<>();
            List<Object> rs = jxDocument.sel("//div[@id='div6']//tr");
            for (int i = 1; i < rs.size(); i++) {
                JSONObject info = new JSONObject();
                info.put("法律法规名称", ((Element) rs.get(i)).child(1).text());
                info.put("法规类型", ((Element) rs.get(i)).child(2).text());
                info.put("条例", ((Element) rs.get(i)).child(3).text());

                //条款内容
                List<Object> clauseList = jxDocument.sel("//div[@id='div6']//div[@class='smltc9']");
                info.put("条款内容", ((Element) clauseList.get(i - 1)).child(1).child(0).text());
                settingBasis.add(info);
            }
            return settingBasis;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    /**
     * 中介服务
     * @param jxDocument
     * @return
     */
    public static JSONObject getAgentService(JXDocument jxDocument){
        try {
            JSONObject agent = new JSONObject();
            String info = null;
            List<Object> list = jxDocument.sel("//div[@id='div7']//p");
            for (Object o : list) {
                info = ((Element) o).text();
            }
            agent.put("中介服务", info);
            return agent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 常见问题
     * @param jxDocument
     * @return
     */
    public static List<JSONObject> getQuestionInfo(JXDocument jxDocument){
        try {
            List<JSONObject> questionList = new ArrayList<>();
            List<Object> list = jxDocument.sel("//div[@id='div8']//tr");
            for (int i = 0; i < list.size(); i++) {
                JSONObject info = new JSONObject();
                String key = ((Element) list.get(i)).child(1).child(0).text();
                String vaule = ((Element) list.get(i)).child(1).child(1).text();
                info.put(key,vaule);
                questionList.add(info);
            }
            return questionList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    public static String fileDownloadAndUploadMinioGet (String url, String docName){
        // docName 文件名要带后缀
        try {
            JSONObject headers = new JSONObject();
            return DownloadAndUploadMinio.getInstance().downloadGet(
                    url,
                    headers,
                    new JSONObject(),
                    docName
            );
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }

    /*******
     *  判断是否为null 或者 空字符串
     * @param str
     * @return
     */
    public static boolean isNotBlankOrNull (Object str){
        return str != null && !str.toString().equals("");
    }
}
